package com.zz.ui_demo_wedgit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.zz.ui_demo_wedgit.adapter.ClassRoomBuildingAdapter;
import com.zz.ui_demo_wedgit.pojo.ClassroomBuilding;

import java.util.ArrayList;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        //数据(data)<>适配器<>界面（spinner)
        //region 学期下拉框：使用spinner静态加载数据
        Spinner spSemester = findViewById(R.id.sp_semester);
        spSemester.setSelection(0,true);
        spSemester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0)
                    return;
                Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion
        //region 学院下拉框：Spinner通过ArrayAdapter加载动态数据,使用系统预定义下拉列表项加载数据
        Spinner spSchool = findViewById(R.id.sp_school);
        String[] school = {"-==选择学院==-", "计算机学院", "理学院", "外国语学院", "建工学院"};
        //设置初始默认选项
        spSchool.setSelection(0,true);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, school);
        spSchool.setAdapter(adapter);
        spSchool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0)
                    return;
                Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion
        //region  年级下拉框，使用ArrayAdapter加载动态数据,使用自定义下拉表项视图
        String[] grades = {"年级", "大一", "大二", "大三", "大四"};
        Spinner spGrade = findViewById(R.id.sp_grade);
        ArrayAdapter<String> gradeAdapter = new ArrayAdapter<>(this, R.layout.my_spinner_item, grades);
        gradeAdapter.setDropDownViewResource(R.layout.my_spinner_dropdown);
        spGrade.setAdapter(gradeAdapter);
        spGrade.setSelection(0,true);
        spGrade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0)
                    return;
                Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion

        //region 教学楼下拉框，通过自定义适配器加载动态数据，使用自定义下拉列表项视图
        ArrayList<ClassroomBuilding> classroomBuildings = new ArrayList<>();
        classroomBuildings.add(new ClassroomBuilding("选择教学楼", "", ""));
        classroomBuildings.add(new ClassroomBuilding("文友楼", "510", "多媒体"));
        classroomBuildings.add(new ClassroomBuilding("文友楼", "618", "多媒体"));
        classroomBuildings.add(new ClassroomBuilding("香园楼", "314", "普通"));
        classroomBuildings.add(new ClassroomBuilding("实验楼", "605", "计算机实验室"));
        Spinner spClassroomBuilding = findViewById(R.id.sp_classroom_building);
        spClassroomBuilding.setAdapter(new ClassRoomBuildingAdapter(this, classroomBuildings));
        spClassroomBuilding.setSelection(0,true);
        spClassroomBuilding.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0)
                    return;
                Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion
    }

}